package com.ws.controller;

import com.github.pagehelper.PageInfo;
import com.ws.domain.Goods;
import com.ws.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private GoodsService goodsService;
    //    新品上市
    @RequestMapping("/new")
    public String newGoods(HttpServletRequest request,
                           @RequestParam(required = false, defaultValue = "1")int page,
                           @RequestParam(required = false, defaultValue = "5")int size
    ) {

        //2、分页查询指定类型商品
        request.setAttribute("goodList", goodsService.newGoods(page, size));
        List<Goods> list = goodsService.newGoods(page, size);

        //3、设置分页信息 新品上市跟所有商品查询其商品数量一样
        PageInfo<Goods> pageInfo  = new PageInfo<>(list);
        request.setAttribute("pageInfo",pageInfo);
        return "goods.jsp";
    }
    @RequestMapping("/detail")
    public String detail(int id, Model m) {
        //1、查询某个商品的详情，跳转到商品详情页显示
        m.addAttribute("good", goodsService.getById(id));
        //2、查询两个今日推荐商品在商品详情页显示
        m.addAttribute("todayList", goodsService.getListByTopType());
        return "detail.jsp";
    }
}
